local _G = getfenv(0);
local AddOn = LibStub("AceAddon-3.0"):GetAddon("EquivalencePoints");
local L = LibStub("AceLocale-3.0"):GetLocale("EquivalencePoints")

local print = function(...) AddOn:PrintFunction(...); end
local debug = function(...) AddOn:DebugFunction(...); end


local pairs, ipairs = pairs, ipairs;
local tonumber = tonumber;
local format, upper, find, gmatch, gsub, strsplit = string.format, string.upper, string.find, string.gmatch, string.gsub, strsplit;

AddOn.LookUps = {};
AddOn.LookUps.CrazyShaman = {
	["AP"] = "ATTACKPOWER",
	["STR"] = "STR",
	["AGI"] = "AGI",
	["STA"] = "STA",
	["CR"] = "CR_CRIT",
	["HR"] = "CR_HIT",
	["HsR"] = "CR_HASTE",
	["RS"] = "CR_RESILIENCE",
	["IA"] = "IGNOREARMOR",
	["ExP"] = "CR_EXPERTISE", -- Misstype should've been ExR but nvm.
	["ExR"] = "CR_EXPERTISE",
};

AddOn.LookUps.LootRank = {
	["STR"] = "STR",
	["ARM"] = "ARMOR",
	["EXP"] = "CR_EXPERTISE",
	["MP5"] = "MANAREG",
	["STA"] = "STA",
	["DEF"] = "CR_DEFENSE",
	["MCR"] = "CR_CRIT", --RANGEDCRIT
	["HEAL"] = "HEAL",
	["AGI"] = "AGI",
	["DOD"] = "CR_DODGE",
	["MHIT"] = "CR_HIT",
	["SPC"] = "CR_SPELLCRIT",
	["INT"] = "INT",
	["BLV"] = "BLOCKVALUE",
	["MAP"] = "ATTACKPOWER", --RANGEDATTACKPOWER
	["SPH"] = "CR_SPELLHIT",
	["SPI"] = "SPI",
	["BLR"] = "CR_BLOCK",
	--["FAP"] = "FAP", --Feral AP
	["SPD"] = "DMG",
	["PAR"] = "CR_PARRY",
	["ARP"] = "IGNOREARMOR",
	["FID"] = "FIREDMG",
	["SCKM"] = "EMPTY_SOCKET_META", -- Meta
	["RES"] = "CR_RESILIENCE",
	["MH"] = "CR_HASTE",
	["FRD"] = "FROSTDMG",
	--["DPS"] = "DPS", -- MH DPS
	["ARD"] = "ARCANEDMG",
	--["ODPS"] = "ODPS", -- OH DPS
	["NAD"] = "NATUREDMG",
	-- ["RDPS"] = "RDPS", --Ranged DPS
	["SHD"] = "SHADOWDMG",
	["HAS"] = "CR_SPELLHASTE",
	["SPP"] = "SPELLPEN",
};

AddOn.LookUps.Lootzor = {
	["hbeu"] = "STR",
	["hcau"] = "STA",
	["hbfu"] = "AGI",
	["hcbu"] = "INT",
	["hccu"] = "SPI",
	["habdu"] = "HEALTHREG",
	["habeu"] = "MANAREG",
	["hecu"] = "CR_CRIT",
	["hedu"] = "CR_HIT",
	["heeu"] = "ATTACKPOWER",
	["hefu"] = "RANGEDATTACKPOWER",
	["hfau"] = "CR_DODGE",
	["hfbu"] = "CR_PARRY",
	["hfcu"] = "CR_BLOCK",
	["hfdu"] = "BLOCKVALUE",
	["hffu"] = "CR_DEFENSE",
	["haaau"] = "CR_RESILIENCE",
	["bdu"] = "ARMOR",
	["heau"] = "CR_SPELLCRIT",
	["hebu"] = "CR_SPELLHIT",
	["hdcu"] = "DMG",
	["hadcu"] = "ARCANEDMG",
	["haddu"] = "FIREDMG",
	["hadeu"] = "FROSTDMG",
	["hadfu"] = "NATUREDMG",
	["haeau"] = "SHADOWDMG",
	["hdfu"] = "HEAL",
	--["hcdu"] = "arcane resist",
	--["hcfu"] = "frost resist",
	--["hceu"] = "fire resist",
	--["hdau"] = "nature resist",
	--["hdbu"] = "shadow resist",
	--["habcu"] = "dps",
	--["haadu"] = "damage low",
	--["haaeu"] = "damage high",
}

function AddOn:CrazyShamanImport(data)
	-- Assume the data will look like this (to avoid locale errors with fraction separators):
	-- AP:100;CR:167;STR:200;AGI:148;HR:175;HsR:169;IA:023;ExP:258;
	
	local standardizedData = {}
	for _, dataPart in ipairs( { strsplit(";", data) } ) do
		if (dataPart and dataPart ~= "") then
			local identifier, number = strsplit(":", dataPart);
			
			local value = tonumber(number / 100);
			local key = self.LookUps and self.LookUps.CrazyShaman and self.LookUps.CrazyShaman[identifier];
			
			if (key and value) then
				standardizedData[key] = value;
			end
		end
	end
	
	self:ImportSet(L["_import Crazy Shaman Simulator"], standardizedData);
end

function AddOn:LootrankImport(URL)
	-- http://www.lootrank.com/wow/rank.asp?Cla=1&Max=10&Slot=0&Gem=3&Art=0&Dif=146&Str=16&Arm=0&Exp=15&mp5=0&Sta=3&Def=0&mcr=20&heal=0&Agi=15&Dod=0&mhit=15&spc=0&Int=0&blv=0&map=7&sph=0&Spi=0&blr=0&fap=0&spd=0&par=0&arp=2&fid=0&Sckm=180&res=0&mh=28&frd=0&dps=100&ard=0&odps=50&nad=0&rdps=0&shd=0&has=0&spp=0&title=DPS%20Warrior
	local _, QueryString = strsplit("?", URL)
	-- Cla=1&Max=10&Slot=0&Gem=3&Art=0&Dif=146&Str=16&Arm=0&Exp=15&mp5=0&Sta=3&Def=0&mcr=20&heal=0&Agi=15&Dod=0&mhit=15&spc=0&Int=0&blv=0&map=7&sph=0&Spi=0&blr=0&fap=0&spd=0&par=0&arp=2&fid=0&Sckm=180&res=0&mh=28&frd=0&dps=100&ard=0&odps=50&nad=0&rdps=0&shd=0&has=0&spp=0&title=DPS%20Warrior
	
	local QueryStringParts = { strsplit("&", QueryString) }
	local SetTable, SetTitle = {}, "";
	for _, Part in ipairs(QueryStringParts) do
		local ID, Val = strsplit("=", Part);
		ID = ID and upper(ID);
		Val = tonumber(Val);
		
		local Key = ID and self.LookUps.LootRank[ID];
		
		if (Key and Val) then
			SetTable[Key] = Val;
		elseif (ID == "TITLE") then
			--title=DPS%20Warrior
			Val = gsub(Val, "%%20", " ");
			Val = gsub(Val, "+", " ");
			SetTitle = format(L["_import Lootrank Title"], Val);
		end
	end
	
	print(L["Lootrank doesn't differ between ranged/melee crit & hit, so if you wanted ranged you should import the set and change it's values! By default the import is to Melee!"]);
	self:ImportSet(format(L["_import Lootrank"], SetTitle), SetTable);
end

function AddOn:LootzorImport(URL)
	-- url = "http://www.lootzor.com/index.php?c=0&l=70&b=0&s=1&r=7&g=3&i=5&z=5&p=bdu18hbeu1hbfu3hcau2hcbu4hccu5hcdu28hceu30hcfu29hdau31hdbu32hdcu21hdfu27heau19hebu20hecu8hedu9heeu10hefu11hfau12hfbu13hfcu14hfdu15hffu16haaau17haadu34haaeu35habcu33habdu6habeu7hadcu22haddu23hadeu24hadfu25haeau26";
	local found, _, QueryString = find(url, "[%?&](p=[^&]+)");
	if (not found) then return; end
	local SetTable = {};
	
	for QueryStringParts in gmatch(qp, "%a+%d+") do
		local found, _, ID, Val = find("bdu18", "(%a+)(%d+)");
		local Key = ID and self.LookUps.Lootzor[ID];
		
		if (found and Key and tonumber(Val)) then
			SetTable[Key] = tonumber(Val);
		end
	end
	
	self:ImportSet(L["_import Lootzor"], SetTable);
end


--[[
			Retarded link
			http://thottbot.com/score/B2,2,,,2W2,,,,2
]]